package com.ziyun.erp.modules.member.service.impl;

import com.ziyun.erp.common.e.member.UserApplyCommissionStatusEnum;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.member.entity.UserBaseInfoEntity;
import com.ziyun.erp.modules.member.service.UserBaseInfoService;
import com.ziyun.erp.modules.member.service.UserShareService;
import io.renren.common.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.ziyun.erp.modules.member.dao.UserApplyCommissionDao;
import com.ziyun.erp.modules.member.entity.UserApplyCommissionEntity;
import com.ziyun.erp.modules.member.service.UserApplyCommissionService;


@Service("userApplyCommissionService")
public class UserApplyCommissionServiceImpl extends ServiceImpl<UserApplyCommissionDao, UserApplyCommissionEntity> implements UserApplyCommissionService {

    @Autowired
    private UserBaseInfoService userBaseInfoService;
    @Autowired
    private UserShareService userShareService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String memberName = StringUtil.getRequestParamString(params.get("memberName"));

        // 会员
        List<Integer> memberIdList = null;
        if(StringUtil.isNotEmpty(memberName)){
            List<UserBaseInfoEntity> userBaseInfoEntityList = userBaseInfoService.selectList(
                    new EntityWrapper<UserBaseInfoEntity>()
                            .eq("`name`",memberName)
            );
            if(userBaseInfoEntityList != null && userBaseInfoEntityList.size() > 0){
                memberIdList = new ArrayList<>();
                for (UserBaseInfoEntity userBaseInfoEntity:userBaseInfoEntityList){
                    memberIdList.add(userBaseInfoEntity.getUserId());
                }
            }
        }

        Page<UserApplyCommissionEntity> page = this.selectPage(
                new Query<UserApplyCommissionEntity>(params).getPage(),
                new EntityWrapper<UserApplyCommissionEntity>()
                        .in(StringUtil.isNotNull(memberIdList),"member_id",memberIdList)
        );

        return new PageUtils(page);
    }

    @Override
    public void audit(UserApplyCommissionEntity userApplyCommissionEntity) {
        Map<String,Object> updateMap = new HashMap<>();
        updateMap.put("status",UserApplyCommissionStatusEnum.TO_PAY.toInt());
        updateMap.put("auditName",UserUtils.getCurentUser().getUserId());
        updateMap.put("auditTime",new Date());

        baseMapper.updateByMap(updateMap,userApplyCommissionEntity.getApplyId());
    }

    @Override
    public void pay(UserApplyCommissionEntity userApplyCommissionEntity) {
        Map<String,Object> updateMap = new HashMap<>();
        updateMap.put("status",UserApplyCommissionStatusEnum.ALREADY_PAY.toInt());
        updateMap.put("payName",UserUtils.getCurentUser().getUserId());
        updateMap.put("payTime",new Date());

        baseMapper.updateByMap(updateMap,userApplyCommissionEntity.getApplyId());

        // 写入也提现佣金
        userShareService.pay(userApplyCommissionEntity.getAmount(),userApplyCommissionEntity.getMemberId());
    }

    @Override
    public void reject(UserApplyCommissionEntity userApplyCommissionEntity) {
        Map<String,Object> updateMap = new HashMap<>();
        updateMap.put("status",UserApplyCommissionStatusEnum.REJECT.toInt());
        baseMapper.updateByMap(updateMap,userApplyCommissionEntity.getApplyId());

        // 返回用户可以提现佣金
        userShareService.reject(userApplyCommissionEntity.getAmount(),userApplyCommissionEntity.getMemberId());
    }

}
